package com.example.blog_system.web.admin;

import com.example.blog_system.model.domain.Article;
import com.example.blog_system.service.IArticleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin")
public class AdminController {
//    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
    @Autowired
    private IArticleService articleServiceImpl;

    @GetMapping(value = {"","/index"})
    public String index(HttpServletRequest request){
        return "back/index.html";
    }

    @GetMapping(value = "/article/toEditPage")
    public String newArticle(){
        return "back/article_edit.html";
    }

    @PostMapping(value = "/article/publish")
    @ResponseBody
    public boolean publishArticle(Article article){
        if (StringUtils.isBlank(article.getCategories())){
            article.setCategories("默认分类");
        }
        try {
            articleServiceImpl.publish(article);
            System.out.println("文章发布成功");
            return true;
        }catch (Exception e){
            System.out.println("文章发布失败");
            return false;
        }
    }
}
